<template>
	<div class="sys-region-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级名称">
							<el-cascader
									:options="regionData"
									:props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'name' }"
									placeholder="请选择上级名称"
									clearable
									class="w100"
									v-model="state.ruleForm.Pid"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>

					<el-col :span="24" class="mb20">
						<el-radio-group v-model="state.selectedOption">
							<el-radio value="管理处">管理处</el-radio>
							<el-radio value="路段">路段</el-radio>
						</el-radio-group>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="state.selectedOption === '管理处'" class="mb20">
						<el-form-item label="管理处名称" prop="name" :rules="[{ required: true, message: '管理处名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" placeholder="管理处名称" clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24"  class="mb20" v-if="state.selectedOption === '路段'">
						<el-form-item label="路线全称" prop="roadname" :rules="[{ required: true, message: '路线全称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.roadname" placeholder="路线全称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20"v-if="state.selectedOption === '路段'">
						<el-form-item label="路段名称" prop="sectionname" :rules="[{ required: true, message: '路段名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.sectionname" placeholder="路段名称" clearable />
						</el-form-item>
					</el-col>

					<el-col :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="路线编号" prop="roadNo">
							<el-input v-model="state.ruleForm.roadNo" placeholder="路线编号" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="技术等级" prop="jslevel">
							<el-input v-model="state.ruleForm.jslevel" placeholder="技术等级" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="路面类型" prop="roadtype">
							<el-input v-model="state.ruleForm.roadtype" placeholder="路面类型" clearable />
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="起点桩号" prop="start_stake_num">
							<el-input v-model="state.ruleForm.start_stake_num" placeholder="起点桩号" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="结束桩号" prop="end_stake_num">
							<el-input v-model="state.ruleForm.end_stake_num" placeholder="结束桩号" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="里程" prop="mileage">
							<el-input v-model="state.ruleForm.mileage" placeholder="里程" clearable />
						</el-form-item>
					</el-col>

					<el-col :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="车道数量(个)" prop="lineNo">
							<el-input v-model="state.ruleForm.lineNo" placeholder="车道数量(个)" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="路基宽度(m)" prop="roadwidth">
							<el-input v-model="state.ruleForm.roadwidth" placeholder="路基宽度(m)" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="8" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="设计时速（km/h）" prop="speed">
							<el-input v-model="state.ruleForm.speed" placeholder="设计时速（km/h）" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="12" v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="路龄（年）" prop="roadage">
							<el-input v-model="state.ruleForm.roadage" placeholder="路龄（年）" clearable />
						</el-form-item>
					</el-col>
					<el-col  :span="12"  v-if="state.selectedOption === '路段'" class="mb20">
						<el-form-item label="通车时间"  prop="openDate" :rules="[{ required: true, message: '通车时间不能为空', trigger: 'blur' }]">
							<el-date-picker v-model="state.ruleForm.openDate" type="date" placeholder="通车时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="选择年度">
							<div class="tree-h-right">
								<el-select v-model="state.selectedYear" placeholder="选择年度" style="width: 100px" >
									<el-option v-for="year in state.yearOptions" :key="year" :value="year" :label="year" />
								</el-select>
							</div>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注内容" clearable type="textarea" />
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>

			<!--	<MapDragRoute ref="mapDragRoute" :title="地图组件" @handleQuery="handleQuery"  :regionData="state.regionData"/>-->

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>

	</div>
</template>

<script lang="ts" setup name="sysEditRegion">
	import { reactive, ref } from 'vue';
	import {ElMessage, UploadInstance} from 'element-plus';
	import { getAPI } from '/@/utils/axios-utils';
	import {SysFileApi, SysRegionApi} from '/@/api-services/api';
	import {SysFile, UpdateRegionInput} from '/@/api-services/models';
	import MapDragRoute from '/@/views/map/MapDragRoute.vue';
	import {uploadFilesPoject ,getjcinfo} from "/@/api/region/region";

	const editRegionRef = ref<InstanceType<typeof MapDragRoute>>();

	const props = defineProps({
		title: String,
		regionData: {
			type: Array,
			required: true,
		},
	});
	const emits = defineEmits(['handleQuery']);
	const ruleFormRef = ref();
	const uploadRef = ref<UploadInstance>();
	const state = reactive({
		fileData: [] as Array<SysFile>,
		selectedOption:"路段",
		fileList: [] as any,
		fileName: '',
		isShowDialog: false,
		dialogUploadVisible: false,
		ruleForm: {} as UpdateRegionInput,
		regionData:[],
		selectedYear: '2024', // 存储所选年度的值
		yearOptions: ['2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032'] // 年度选项数组
	});


	// 打开上传页面
	const openUploadDialog = () => {
		state.fileList = [];
		state.dialogUploadVisible = true;
	};

	// 通过onChanne方法获得文件列表
	const handleChange = (file: any, fileList: []) => {
		state.fileList = fileList;
	};

	// 上传
	const uploadFile = async () => {
		state.dialogUploadVisible = false;
	};

	// 打开弹窗
	const openDialog = async (row: any) => {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.isShowDialog = true;
		ruleFormRef.value?.resetFields();
	};



	// 关闭弹窗
	const closeDialog = () => {
		emits('handleQuery');
		state.isShowDialog = false;

	};

	// 取消
	const cancel = () => {
		state.fileList = [];
		state.isShowDialog = false;
	};

	// 提交
	const submit = () => {
		ruleFormRef.value.validate(async (valid: boolean) => {
			if (!valid) return;
			const pid = null;

			if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
				state.ruleForm.year=state.selectedYear;
				console.log("子组件接住的年度",state.ruleForm.year)
				console.log("看一下提交的数据",state.ruleForm)

				const payload = {
					id: state.ruleForm.id,
					code: state.ruleForm.code,
					name: state.ruleForm.name,
					year: state.selectedYear
				};

				console.log("子组件接住的年度", payload.year);
				console.log("看一下提交的数据", payload);

				// 传递新对象
				await getjcinfo(payload);

			} else {
				if(state.ruleForm.Pid==1){
					state.ruleForm.Pid=0;
				}
				console.log("看下",state.selectedOption)
				if(state.selectedOption=="路段"){
					state.ruleForm.Level=4;
					state.ruleForm.name=state.ruleForm.sectionname
				}

				await getAPI(SysRegionApi).apiSysRegionAddPost(state.ruleForm);
			}
			console.log("查看当前提交的表单数据",state.ruleForm);
			closeDialog();
		});
	};

	// 导出对象
	defineExpose({ openDialog });
</script>
